package cuttings;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;

import java.awt.event.*;
import javax.swing.table.*;
import javax.swing.table.AbstractTableModel;

import java.util.Vector;



public class Menu extends JPanel implements Cuttings_Commons {

	public static JLayeredPane layeredPane; 
	private int last_x=0,last_y=0;

	//Menu Items
	private boolean ingame = false;
	private boolean menu= false;
	private boolean awtToggle = true;
	private boolean options = false;
	private boolean servers = false;
	private static boolean preGame = false;
	private boolean login= true;
	private static boolean invite = false;
	private static boolean endGame = false;

	public static boolean showImage = false;
	Image img;
	public static BufferedImage img2 = null;
	public boolean newuser = false;
	public boolean musicshowfile = false;
	public static boolean showfile = false;
	public boolean forgot = false;
	JLabel Pwd;
	String putPwd = "Oolala";

	public static JPanel base1; 
	public static JPanel gamePanel;
	public static backgroundLayout base;

	public TCPClient status;
	
	
	
	private final static String musicFolder = "cuttings/sounds/";
	private final String menuBanner = "images/haywards.png";
	private final String bgmFile = musicFolder + "gbutheme.mp3";
	private final String mouseClick = musicFolder + "MouseClick.mp3";
	private final static String victory = musicFolder + "victory.mp3";
	private final static String lose = musicFolder + "lose.mp3";
	private static String[] playlist = {musicFolder + "MouseClick.mp3",musicFolder + "MouseClick.mp3",musicFolder + "MouseClick.mp3", musicFolder+"Counter Strike 16 Background Music.mp3"}; 

	public static String gameID;
	public static String hostname;
	static public JTable table;
	static public JTextArea chat;
	public JTextField message;
	public static JList onlinePlayerlist;
	public static JList attackers;
	public static JList defenders;

	private static JList blackbox; //for hiding accpeted invite button
	private static JLabel invitedGame;
	public static ServerListModel data;


	private static MP3 bgm;
	private MP3 click;
	private static MP3 gameBgm;
	private static MP3 endGameBgm;

	static public String[] playersList = {"No active players", ""};
	static public String[] attackerList = new String[100];
	static public String[] defenderList = new String[100];

	//playersList[0] = "No Users Online";

	public static boolean gameStart =false;

	/*static public Object[][] serverList = {
	  {"Kathy", "Smith",
	  "Snowboarding", new Integer(5), new Boolean(false)},
	  {"John", "Doe",
	  "Rowing", new Integer(3), new Boolean(true)},
	  {"Sue", "Black",
	  "Knitting", new Integer(2), new Boolean(false)},
	  {"Jane", "White",
	  "Speed reading", new Integer(20), new Boolean(true)},
	  {"Joe", "Brown",
	  "Pool", new Integer(10), new Boolean(false)}
	  };*/

	static public Object[][] serverList = {
		{"No active Games", ""},

	};
	
	static public Object[][] serverList2 = {
		{"No active Games", "lala"},

	};

	static public String[] columnNames = {"First Name",
		"Last Name"
	};



	//Labels and Text Fields
	JTextField Input1 = new JTextField ();
	JTextField Input2 = new JTextField ();
	JTextField Input3 = new JPasswordField ();
	JTextField Name1_ = new JTextField ();
	JTextField Pwd1_ = new JPasswordField ();

	JTextField email = new JTextField();
	JLabel email_ = new JLabel("Email:");

	JLabel Echo1 = new JLabel ("Name:"); 
	JLabel Echo2 = new JLabel ("Username:"); 
	JLabel Echo3 = new JLabel ("Password:"); 
	JLabel Echo4 = new JLabel ("LOGIN"); 

	//Options
	private FileOutput fileOP;
	private String getName;
	private String getUser;
	private String getPwd;
	private String writeData;

	//Login Page
	private static String getLoginName;
	private String getLoginPwd;
	private String getData;
	private String file_data;

	public String file_name = "Invalid";
	public String musicfile_name;

	public String image_name;
	public String file_name2;
	public String musicfile_name2;

	public String newUser;
	public String newPwd;
	public String newEmail;



	public Menu(){

		//Adding Mouse Listeners
		addMouseListener(new Madapter());

		chat = new JTextArea ();

		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		//Create and set up the layered pane.
		layeredPane = new JLayeredPane();
		layeredPane.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));


		add(layeredPane);
		base = new backgroundLayout();
		mainMenu();
		loginMenu();




		bgm = new MP3(bgmFile);
		click = new MP3(mouseClick);
		gameBgm = new MP3("");
		//gameBgm.setPlaylist(playlist);
		bgm.play(1);
	}


	public static BufferedImage profilePic()
	{
		return img2;
	}
	
/*********************************************
/
/ Menu Panels
/
/*********************************************/


	public void loginMenu()
	{
		login = true;
		options = false;
		menu = false;
		base1 = new JPanel();
		base1.setLayout(null);
		base.change("login");

		if(forgot)
		{
			putPwd = "Password : " + putPwd;
			Pwd = new JLabel(putPwd);
			Pwd.setForeground (Color.white);
			Pwd.setBounds(100, 50, 120, 30);
			base1.add(Pwd);
		}

		if(newuser)
		{
			email_.setForeground (Color.white);
			email.setForeground (Color.black);
			email_.setBounds(100, 220, 120, 30);
			email.setBounds(170, 220, 120, 30);
			base1.add(email_);
			base1.add(email);
		}


		Echo2.setForeground (Color.white);
		Name1_.setForeground (Color.black);
		Echo4.setForeground (Color.white);
		Echo3.setForeground (Color.white);
		Pwd1_.setForeground (Color.black);

		Echo2.setBounds(100, 160, 120, 30);
		Name1_.setBounds(170, 160, 120, 30);
		Echo4.setBounds(170, 100, 150, 30);
		Echo3.setBounds(100, 190, 120, 30);
		Pwd1_.setBounds(170, 190, 120, 30);


		base1.add(Pwd1_);
		base1.add(Echo3);
		base1.add(Name1_);
		base1.add(Echo2);
		base1.add(Echo4);

		base1.setBounds(200, 50, GAME_WIDTH-400, GAME_HEIGHT-100);
		base1.setOpaque(false);
		layeredPane.add(base1, new Integer(1), 0);
		layeredPane.repaint();
	}

	public void optionsMenu(){
		options = true;
		menu=false;
		base1 = new JPanel();
		base1.setLayout(null);
		base.change("options");
		//base.temp();

		/* Decoration */
		//setBackground (Color.yellow);
		Input1.setBackground (Color.green);
		Echo1.setForeground (Color.white);

		Input2.setBackground (Color.green);
		Echo2.setForeground (Color.white);

		//Dimension e = .getPreferredSize();

		//(x, y, xlength, ylength)
		Echo1.setBounds(100, 100, 120, 30);
		Input1.setBounds(170, 100, 120, 30);

		Echo2.setBounds(100, 160, 120, 30);
		Input2.setBounds(170, 160, 120, 30);


		if(showfile)
		{
			file_name2 = "Image: " + file_name;
			JLabel filename = new JLabel(file_name2);
			filename.setForeground (Color.white);
			filename.setBounds(100, 200, 220, 30);
			base1.add(filename);
		}

		if(musicshowfile)
		{
			musicfile_name2 = "Music: " + musicfile_name;
			JLabel mfilename = new JLabel(musicfile_name2);
			mfilename.setForeground (Color.white);
			mfilename.setBounds(100, 220, 220, 30);
			base1.add(mfilename);
		}

		base1.add(Input2);
		base1.add(Echo2);
		base1.add(Input1);
		base1.add(Echo1);

		base1.setBounds(200, 50, GAME_WIDTH-400, GAME_HEIGHT-100);
		base1.setOpaque(false);
		layeredPane.add(base1, new Integer(1), 0);
		layeredPane.repaint();


	}

	public void serversMenu(){
		servers = true;
		options = false;
		menu=false;
		base1 = new JPanel();
		base1.setLayout(null);
		base.change("servers");
		//base.temp();

		JTextField Input1 = new JTextField ();
		JLabel PlayerTitle = new JLabel ("Players"); 

		/* Decoration */
		//setBackground (Color.yellow);
		//Echo1.setForeground (Color.white);
		//Dimension e = .getPreferredSize();

		//(x, y, xlength, ylength)
		PlayerTitle.setBounds(620, 50, 100, 30);
		PlayerTitle.setForeground (Color.white);



		onlinePlayerlist = new JList(playersList); //data has type Object[]
		onlinePlayerlist.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		onlinePlayerlist.setLayoutOrientation(JList.VERTICAL);
		onlinePlayerlist.setVisibleRowCount(4);
		onlinePlayerlist.setBounds(620, 150, 100, 100);
		onlinePlayerlist.setBackground (Color.green);

		//updateOnlinePlayers(playersList);


		JScrollPane listScroller = new JScrollPane(onlinePlayerlist);
		listScroller.setPreferredSize(new Dimension(250, 80));

		//table = new JTable(serverList, columnNames);


		data = new ServerListModel();
		table = new JTable(data);

		table.setBounds(100, 150,450,150);
		
		//data.updateServersList(serverList);

		

		table.addMouseListener(new MouseAdapter(){
				public void mouseClicked(MouseEvent e){
				if (e.getClickCount() == 2){

				System.out.println(" double click " +  table.getValueAt(table.getSelectedRow(),0));
				}
				}
				} );


		//Horrible hack to hide invite button
		blackbox = new JList();
		blackbox.setBounds(250, 325, 160, 50);
		blackbox.setBackground (new Color(255, 0, 0));

		invitedGame = new JLabel("Join " +hostname);
		invitedGame.setFont(new Font("Arial", Font.BOLD, 18));
		invitedGame.setBounds(285, 270, 100, 150);
		invitedGame.setForeground (Color.black);
		if (invite) blackbox.setVisible(false);


		//UserRole.getHostList(); //return string

		base1.add(blackbox);
		base1.add(invitedGame);

		base1.add(PlayerTitle);
		base1.add(onlinePlayerlist);
		base1.add(table);
		base1.add(listScroller);
		base1.setBounds(0, 0, GAME_WIDTH, GAME_HEIGHT);
		base1.setOpaque(false);
		layeredPane.add(base1, new Integer(1), 0);
		layeredPane.repaint();

		//popupInvite("user1","1"); //test call


	}

	public static String popupInvite(String host, String gameid){

		//1 = No
		//0 = Yes
		//-1 = Close button
		
		int n = JOptionPane.showConfirmDialog(
				base1,
				"Would you like to accept the invite from " + host,
				"Game invitation",
				JOptionPane.YES_NO_OPTION,
				JOptionPane.QUESTION_MESSAGE);

		gameID = gameid;
		hostname = host;
		invitedGame.setText("Join " +hostname);

		System.out.println(n); //debug output

		switch(n){
			case -1:
			case 1: 
				return "#:invitationrejected:"+host+gameid;
				//insert code to reply to server with rejection.
			case 0: 
				invite = true;
				blackbox.setVisible(false);
				try{
					UserRole.acceptInvitation(hostname,getLoginName,gameID);
				}
				catch (Exception e) {}
				return "#:invitationaccepted:"+host+gameid;
		}

		return "";

	}

	public void preGameMenu(){
		options = true;
		menu=false;
		base1 = new JPanel();
		base1.setLayout(null);
		base.change("preGame");
		//base.temp();

		JLabel Echo1 = new JLabel ("Name"); 

		/* Decoration */
		//setBackground (Color.yellow);
		//Input1.setBackground (Color.green);
		//Echo1.setForeground (Color.white);
		//Dimension e = .getPreferredSize();

		//(x, y, xlength, ylength)
		Echo1.setBounds(100, 100, 100, 30);
		//Input1.setBounds(150, 100, 100, 30);


		attackers = new JList(attackerList); //data has type Object[]
		attackers.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		attackers.setLayoutOrientation(JList.VERTICAL);
		attackers.setVisibleRowCount(-1);
		attackers.setBounds(125, 125, 100, 100);
		attackers.setBackground (new Color(5, 75, 25));
		attackers.setForeground(Color.white);

		defenders = new JList(defenderList); //data has type Object[]
		defenders.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		defenders.setLayoutOrientation(JList.VERTICAL);
		defenders.setVisibleRowCount(-1);
		defenders.setBounds(GAME_WIDTH-375, 125, 100, 100);
		defenders.setBackground (new Color(5, 25, 25));
		defenders.setForeground(Color.white);

		onlinePlayerlist = new JList(playersList); //data has type Object[]
		onlinePlayerlist.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		onlinePlayerlist.setLayoutOrientation(JList.VERTICAL);
		onlinePlayerlist.setVisibleRowCount(4);
		onlinePlayerlist.setBounds(620, 150, 100, 100);
		onlinePlayerlist.setBackground (Color.green);

		MouseListener mouseListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					int index = onlinePlayerlist.locationToIndex(e.getPoint());
					String temp = (String)onlinePlayerlist.getSelectedValue();
					System.out.println("Double clicked on user " + temp );

					try {
						UserRole.sendInvite(temp); //set to bold
					} 
					catch (Exception ex)
					{
						System.out.println(e);
					}
				}
			}
		};
		onlinePlayerlist.addMouseListener(mouseListener);

		//chat = new JTextArea ();
		//updateChatWindow("poda");
		chat.setBounds(100, GAME_HEIGHT-180, 500, 75);
		chat.setEditable(false);

		message = new JTextField ();
		message.setBounds(100, GAME_HEIGHT-100, 500, 25);

		System.out.println("hostname is " + hostname + " and user is " + getLoginName);

		if (!getLoginName.equals(hostname)){
			blackbox = new JList();
			blackbox.setBounds(265, GAME_WIDTH/2 - 100, 101, 50);
			blackbox.setBackground (Color.black);
			base1.add(blackbox);
		}

		base1.add(attackers);
		base1.add(defenders);
		base1.add(onlinePlayerlist);

		base1.add(chat);
		base1.add(message);

		base1.setBounds(0, 0, GAME_WIDTH, GAME_HEIGHT);
		base1.setOpaque(false);
		layeredPane.add(base1, new Integer(1), 0);
		layeredPane.repaint();

		//removePanel("test");

	}

	static public void updateChatWindow(String text){
		chat.setText(text+"\n"+chat.getText());
		//System.out.println("testing shiva");
	}

	static public void updateOnlinePlayers(String text[]){
		playersList=text;
		onlinePlayerlist.setListData(playersList);
	}

	static public void updateServerList(String text[][]){
		data.updateServersList(text);
		//table.data=text;
		//table = new JTable(serverList, columnNames);
	}

	//Changed from array of strings to vector
	/*
	   static public void updateAttackers(String text[]){
	   attackerList=text;
	   for(int i=0;i<attackerList.length;i++)
	   System.out.println("Printing updateAttackers " + i +" = " + attackerList[i]);

	   attackers.setListData(attackerList);
	   }
	 */

	static public void updateAttackers(String text[]){
		System.out.println("updateAttackers function called");
		attackerList=text;
		if (preGame) attackers.setListData(attackerList);
	}

	static public void updateDefenders(String text[]){
		System.out.println("updateDefenders function called");
		defenderList=text;
		//if (defenderList.length<1) defenderList[0]="Empty"};
		if (preGame) defenders.setListData(defenderList);
	}

	public static void startGame(){
		
		gameBgm.setPlaylist(playlist);
		
		
		

		
		bgm.stop();
		gameBgm.play(2);
		
		
		
		removePanel("login");
		//base.change("endGame");
		System.out.println("Inside startgame func");
		base1 = new Game();
		base1.setFocusable(true);
		base1.grabFocus();
		base1.setBounds(0, 0, GAME_WIDTH, GAME_HEIGHT);
		Game.resetGame();
		layeredPane.add(base1, new Integer(1), 0);
		layeredPane.repaint();
		
		//UserRole.gameWinLose("lose");
	}
	
	public static void endGame(String status, Hashtable<String,Integer> attackerScorers,  Hashtable<String,Integer> defendersScorers){
	//public static void endGame(String status) {

		System.out.println("Inside endgame func");
		removePanel("login");
		base.change("endGame");
		//gamePanel = null;
		//layeredPane.remove(1);
		//layeredPane.repaint();
		
		
		base1 = new JPanel();
		base1.setFocusable(true);
		base1.grabFocus();
		base1.setBounds(0, 0, GAME_WIDTH, GAME_HEIGHT);
		base1.setLayout(null);
		base1.setOpaque(false);
		
		JLabel Title = new JLabel ("Victory!"); 
		
		if (status.equals("win")){
			//JLabel Title = new JLabel ("Victory!"); 
			Title.setFont(new Font("Arial", Font.BOLD, 25));
			Title.setForeground(Color.white);
			Title.setBounds(355, 150, 100, 30);
			endGameBgm = new MP3(victory);
		}
		else if (status.equals("lose")){
			Title = new JLabel ("Fail..."); 
			Title.setFont(new Font("Arial", Font.BOLD, 25));
			Title.setForeground(Color.white);
			Title.setBounds(370, 150, 100, 30);
			endGameBgm = new MP3(lose);
		}
		
		//Call top scorers list.
		
		//Attackers
        int no_of_attackers = attackerScorers.size();
        String attack[][] = new String[no_of_attackers][2];

        Enumeration keys = attackerScorers.keys();


        int i=0;
        String attacker;
        Object temp;

        while(keys.hasMoreElements())
        {
            attacker = keys.nextElement().toString();

            attack[i][0] = attacker;
			int t =  attackerScorers.get(attacker);
            attack[i][1] = "" + t;
            i++;
        }
		
		//Defenders
        int no_of_defenders = defendersScorers.size();
        String defend[][] = new String[no_of_defenders][2];

        keys = defendersScorers.keys();


        i=0;
        String defender;

        while(keys.hasMoreElements())
        {
            defender = keys.nextElement().toString();

            defend[i][0] = defender;
			int t =  defendersScorers.get(defender);
            defend[i][1] = "" + t;
            i++;
        }
		
		
		
		JLabel Heading = new JLabel ("Top Scorers"); 
		Heading.setFont(new Font("Arial", Font.BOLD, 18));
		Heading.setForeground(Color.white);
		Heading.setBounds(350, 220, 150, 30);
		
		JLabel attackTitle = new JLabel ("Attackers"); 
		attackTitle.setFont(new Font("Arial", Font.BOLD, 14));
		attackTitle.setForeground(Color.white);
		attackTitle.setBounds(290, 250, 150, 30);
		
		JLabel defendTitle = new JLabel ("Defenders"); 
		defendTitle.setFont(new Font("Arial", Font.BOLD, 14));
		defendTitle.setForeground(Color.white);
		defendTitle.setBounds(440, 250, 150, 30);
		
			
		JTable topscore = new JTable(attack, columnNames);
		topscore.setBounds(250, 280, 145, 150);
		topscore.setBackground (Color.green);
		
		JTable topscore2 = new JTable(defend, columnNames);
		topscore2.setBounds(405, 280, 145, 150);
		topscore2.setBackground (Color.green);
		
		
		base1.add(Title);
		base1.add(topscore);
		base1.add(topscore2);
		base1.add(Heading);
		base1.add(attackTitle);
		base1.add(defendTitle);
		
		layeredPane.add(base1, new Integer(1), 0);
		layeredPane.repaint();
		
		gameBgm.stop();
		endGameBgm.play(0);
		
	}


	public void mainMenu(){
		layeredPane.add(base, new Integer(0), 0);
		layeredPane.repaint();
	}


	public static void removePanel(String panel){
		/*JPanel mainPanel = (JPanel)this.getParent();
		  mainPanel.remove(this);
		  mainPanel.validate();

		  mainPanel.repaint();
		 */
		base1.setVisible(false);

	}


//////

static class ServerListModel extends AbstractTableModel {


	public static Object[][] data = {
		{"No active Games", ""},

	};

	private String[] columnNames= {"First Name",
		"Last Name"
	};

	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return data.length;
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		return data[row][col];
	}

	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	/*
	 * Don't need to implement this method unless your table's
	 * editable.
	 */
	public boolean isCellEditable(int row, int col) {
		//Note that the data/cell address is constant,
		//no matter where the cell appears onscreen.
		if (col < 2) {
			return false;
		} else {
			return true;
		}
	}

	/*
	 * Don't need to implement this method unless your table's
	 * data can change.
	 */
	public void setValueAt(String text[][]) {
		data = text;
		fireTableDataChanged();
	}

	public void updateServersList(Object text[][]){
		data = text;
		fireTableDataChanged();
		//table = new JTable(serverList, columnNames);
	}
}

/////

public class backgroundLayout extends JPanel implements Cuttings_Commons { 

	private Dimension d;
	private ArrayList opponents;
	private Player player;
	private OptionsMenu optionsMenu;
	private Shot shot;

	private int opponentX = 150;
	private int opponentY = 5;
	private int direction = -1;
	private int deaths = 0;

	//Menu Items
	private boolean ingame = false;
	private boolean menu= true;
	private boolean options= false;

	//Options
	private JTextField Input;
	private JLabel Echo;
	//private LayoutManager Layout;

	//private final String expl = "images/explosion.png";
	private final String menuBanner = "images/haywards.png";
	private String message = "Game Over";

	private char letter ='\0';

	private int last_x=0,last_y=0;

	private Thread animator;

	public backgroundLayout() 
	{

		setFocusable(true);
		setBounds(0, 0, GAME_WIDTH, GAME_HEIGHT);
		d = new Dimension(GAME_WIDTH, GAME_HEIGHT);
		setBackground(Color.black);

		//optionsList();


		repaint(); 
		setDoubleBuffered(true);

	}


	public void change(String value)
	{ 
		menu = options = servers = preGame = login = endGame = false;

		if (value == "options"){
			options=true;
		}
		if (value == "menu"){
			menu=true;
		}
		if (value == "servers"){
			servers=true;
		}
		if (value == "preGame"){
			preGame=true;
		}
		if (value == "login")
		{
			login = true;
		}
		if (value == "endGame")
		{
			endGame = true;
		}
		repaint();

	}


	public void paint(Graphics g)
	{
		super.paint(g);

		if(menu) {
			gameMenu(g);
		}
		else if (login)
		{
			login(g);
		}

		else if (options) {
			options(g);
		}

		else if (servers) {
			servers(g);
		}
		else if (preGame) {
			preGame(g);
		}
		else if (endGame) {
			endGame(g);
		}

		Toolkit.getDefaultToolkit().sync();
		g.dispose();
	}
	public void login(Graphics g)
	{

		ImageIcon ii = new ImageIcon(this.getClass().getResource(menuBanner));

		ii.paintIcon(this, g, 0, 0);

		g.setColor(new Color(116, 29, 29));
		g.fillRect(200, 50, GAME_WIDTH-400, GAME_HEIGHT-100);
		g.setColor(Color.white);
		g.drawRect(200, 50, GAME_WIDTH-400, GAME_HEIGHT-100);

		Font small = new Font("Helvetica", Font.BOLD, 14);
		FontMetrics metr = g.getFontMetrics(small);

		g.setColor(Color.white);
		g.setFont(small);

		if(!newuser)
		{
			g.setColor(new Color(0, 32, 48));
			g.fillRect(300, GAME_WIDTH/2 - 110, GAME_WIDTH-600, 50);
			g.setColor(Color.white);
			g.drawRect(300, GAME_WIDTH/2 - 110, GAME_WIDTH-600, 50);
			g.drawString("New User", (GAME_WIDTH/2 - 30), 
					((GAME_HEIGHT/3*2) - 80));

			//Box for Forgot Password
			g.setColor(new Color(0, 32, 48));
			g.fillRect(300, GAME_WIDTH/2 - 50, GAME_WIDTH-600, 50);
			g.setColor(Color.white);
			g.drawRect(300, GAME_WIDTH/2 - 50, GAME_WIDTH-600, 50);
			g.drawString("Forgot Password", (GAME_WIDTH/2 - 50), 
					((GAME_HEIGHT/3*2) - 20));
		}
		//Box for Connect
		g.setColor(new Color(0, 32, 48));
		g.fillRect(300, GAME_WIDTH/2 + 10, GAME_WIDTH-600, 50);
		g.setColor(Color.white);
		g.drawRect(300, GAME_WIDTH/2 + 10, GAME_WIDTH-600, 50);
		g.drawString("Connect", (GAME_WIDTH/2 - 25), 
				((GAME_HEIGHT/3*2)+40));


		//Box for Back
		g.setColor(new Color(0, 32, 48));
		g.fillRect(300, GAME_WIDTH/2 + 70, GAME_WIDTH-600, 50);
		g.setColor(Color.white);
		g.drawRect(300, GAME_WIDTH/2 + 70, GAME_WIDTH-600, 50);

		g.drawString("Exit", (GAME_WIDTH/2 - 14), 
				((GAME_HEIGHT/3*2)+100));    

		/*

		   String test = "Key pressed is " + last_x + " " + last_y;
		   g.drawString(test,100,100);*/


	}


	public void gameMenu(Graphics g)
	{

		ImageIcon ii = new ImageIcon(this.getClass().getResource(menuBanner));

		ii.paintIcon(this, g, 0, 0);


		Font small = new Font("Helvetica", Font.BOLD, 14);
		FontMetrics metr = g.getFontMetrics(small);

		g.setColor(Color.white);
		g.setFont(small);
		//g.drawString("CUTTINGS", (GAME_WIDTH/2), 
		//(GAME_HEIGHT/4));

		//Box for Start Game
		g.setColor(new Color(0, 32, 48));
		g.fillRect(300, GAME_WIDTH/2 - 30, GAME_WIDTH-600, 50);
		g.setColor(Color.white);
		g.drawRect(300, GAME_WIDTH/2 - 30, GAME_WIDTH-600, 50);


		//Box for Options
		g.setColor(new Color(0, 32, 48));
		g.fillRect(300, GAME_WIDTH/2 + 20, GAME_WIDTH-600, 50);
		g.setColor(Color.white);
		g.drawRect(300, GAME_WIDTH/2 + 20, GAME_WIDTH-600, 50);

		//Box for End Game
		g.setColor(new Color(0, 32, 48));
		g.fillRect(300, GAME_WIDTH/2 + 70, GAME_WIDTH-600, 50);
		g.setColor(Color.white);
		g.drawRect(300, GAME_WIDTH/2 + 70, GAME_WIDTH-600, 50);

		g.drawString("Game Lobby", (GAME_WIDTH/2 - 40), 
				(GAME_HEIGHT/3*2));    

		g.drawString("Options", (GAME_WIDTH/2 - 25), 
				((GAME_HEIGHT/3*2)+50));

		g.drawString("Quit", (GAME_WIDTH/2 - 14), 
				((GAME_HEIGHT/3*2)+100));                

		/*    String test = "Key pressed is " + last_x + " " + last_y;
		      g.drawString(test,100,100);*/

	}    

	BufferedImage createResizedCopy(Image originalImage, 
			int scaledWidth, int scaledHeight, 
			boolean preserveAlpha)
	{
		//System.out.println("resizing...");
		int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
		BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
		Graphics2D g = scaledBI.createGraphics();
		if (preserveAlpha) {
			g.setComposite(AlphaComposite.Src);
		}
		g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null); 
		g.dispose();
		return scaledBI;
	}
	
	public void options(Graphics g)
	{

		ImageIcon ii = new ImageIcon(this.getClass().getResource(menuBanner));

		ii.paintIcon(this, g, 0, 0);

		g.setColor(new Color(116, 29, 29));
		g.fillRect(200, 50, GAME_WIDTH-400, GAME_HEIGHT-100);
		g.setColor(Color.white);
		g.drawRect(200, 50, GAME_WIDTH-400, GAME_HEIGHT-100);

		Font small = new Font("Helvetica", Font.BOLD, 14);
		FontMetrics metr = g.getFontMetrics(small);

		g.setColor(Color.white);
		g.setFont(small);



		//Box for Save
		g.setColor(new Color(0, 32, 48));
		g.fillRect(300, GAME_WIDTH/2 + 10, GAME_WIDTH-600, 50);
		g.setColor(Color.white);
		g.drawRect(300, GAME_WIDTH/2 + 10, GAME_WIDTH-600, 50);
		g.drawString("Save", (GAME_WIDTH/2 - 14), 
				((GAME_HEIGHT/3*2)+40));




		//Box for Back
		g.setColor(new Color(0, 32, 48));
		g.fillRect(300, GAME_WIDTH/2 + 70, GAME_WIDTH-600, 50);
		g.setColor(Color.white);
		g.drawRect(300, GAME_WIDTH/2 + 70, GAME_WIDTH-600, 50);

		g.drawString("Back", (GAME_WIDTH/2 - 14), 
				((GAME_HEIGHT/3*2)+100)); 

		//Box for Upload Image
		g.setColor(new Color(0, 32, 48));
		g.fillRect(250, GAME_WIDTH/2 - 50, GAME_WIDTH-650, 50);
		g.setColor(Color.white);
		g.drawRect(250, GAME_WIDTH/2 - 50, GAME_WIDTH-650, 50);

		g.drawString("Upload Image", (GAME_WIDTH/2 - 120), 
				((GAME_HEIGHT/3*2)-20));    

		//Box for Upload Music
		g.setColor(new Color(0, 32, 48));
		g.fillRect(400, GAME_WIDTH/2 -50, GAME_WIDTH-650, 50);
		g.setColor(Color.white);
		g.drawRect(400, GAME_WIDTH/2-50, GAME_WIDTH-650 , 50);

		g.drawString("Upload Music", (GAME_WIDTH/2 + 30), 
				((GAME_HEIGHT/3*2)-20)); 

		if(showImage)
		{
			Image img = null;
			
			try {
				img = ImageIO.read(new File(file_name));
			} catch (IOException e) {
			}

			img2 = createResizedCopy(img, 65, 65, true);


			ImageIcon ij = new ImageIcon(img2);

			//width = ij.getImage().getWidth(null); 

			ij.paintIcon(this, g, 260, 280);

		}    

		/*

		   String test = "Key pressed is " + last_x + " " + last_y;
		   g.drawString(test,100,100);
		 */

	}

	public void endGame(Graphics g)
	{	System.out.println("In endGame AWT");

		ImageIcon ii = new ImageIcon(this.getClass().getResource(menuBanner));

		ii.paintIcon(this, g, 0, 0);
		g.setColor(Color.yellow);
		g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);


		g.setColor(new Color(116, 29, 29));
		g.fillRect(200, 50, GAME_WIDTH-400, GAME_HEIGHT-100);
		g.setColor(Color.white);
		g.drawRect(200, 50, GAME_WIDTH-400, GAME_HEIGHT-100);

		Font small = new Font("Helvetica", Font.BOLD, 14);
		FontMetrics metr = g.getFontMetrics(small);

		g.setColor(Color.white);
		g.setFont(small);


/*
		//Box for Save
		g.setColor(new Color(0, 32, 48));
		g.fillRect(300, GAME_WIDTH/2 + 10, GAME_WIDTH-600, 50);
		g.setColor(Color.white);
		g.drawRect(300, GAME_WIDTH/2 + 10, GAME_WIDTH-600, 50);
		g.drawString("Save", (GAME_WIDTH/2 - 14), 
				((GAME_HEIGHT/3*2)+40));*/




		//Box for Back
		g.setColor(new Color(0, 32, 48));
		g.fillRect(300, GAME_WIDTH/2 + 70, GAME_WIDTH-600, 50);
		g.setColor(Color.white);
		g.drawRect(300, GAME_WIDTH/2 + 70, GAME_WIDTH-600, 50);

		g.drawString("Back to Main Menu", (GAME_WIDTH/2 - 60), 
				((GAME_HEIGHT/3*2)+100)); 


	}


	public void servers(Graphics g)
	{

		ImageIcon ii = new ImageIcon(this.getClass().getResource(menuBanner));

		ii.paintIcon(this, g, 0, 0);

		//Main Frame
		g.setColor(new Color(0, 0, 0));
		g.fillRect(25, 25, GAME_WIDTH-50, GAME_HEIGHT-75);
		g.setColor(Color.white);
		g.drawRect(25, 25, GAME_WIDTH-50, GAME_HEIGHT-75);

		// Outer Box for servers list
		g.setColor(new Color(255, 0, 0));
		g.fillRect(50, 100, GAME_WIDTH-250, GAME_HEIGHT-300);
		g.setColor(Color.white);
		g.drawRect(50, 100, GAME_WIDTH-250, GAME_HEIGHT-300);

		// Outer Box for users list
		g.setColor(new Color(0, 255, 0));
		g.fillRect(610, 100, 140, GAME_HEIGHT-300);
		g.setColor(Color.white);
		g.drawRect(610, 100, 140, GAME_HEIGHT-300);

		Font small = new Font("Helvetica", Font.BOLD, 14);
		FontMetrics metr = g.getFontMetrics(small);

		g.setColor(Color.white);
		g.setFont(small);


		//Box for Accept invite
		g.setColor(new Color(120, 255,112));
		g.fillRect(250, 325, 150, 40);
		g.setColor(Color.white);
		g.drawRect(250, 325, 150, 40);





		//Box for Create Game
		g.setColor(new Color(0, 32, 48));
		g.fillRect(300, GAME_WIDTH/2 + 20, GAME_WIDTH-600, 50);
		g.setColor(Color.white);
		g.drawRect(300, GAME_WIDTH/2 + 20, GAME_WIDTH-600, 50);

		//Box for Back
		g.setColor(new Color(0, 32, 48));
		g.fillRect(300, GAME_WIDTH/2 + 70, GAME_WIDTH-600, 50);
		g.setColor(Color.white);
		g.drawRect(300, GAME_WIDTH/2 + 70, GAME_WIDTH-600, 50);

		/*g.drawString("Send", (645), 
		  (GAME_WIDTH/2 + 90));*/


		g.drawString("Create Game", (GAME_WIDTH/2 - 37), 
				((GAME_HEIGHT/3*2)+50));

		g.drawString("Back", (GAME_WIDTH/2 - 14), 
				((GAME_HEIGHT/3*2)+100));    


		/*
		   String test = "Key pressed is " + last_x + " " + last_y;
		   g.drawString(test,100,100);
		 */

	}
	public void preGame(Graphics g)
	{

		ImageIcon ii = new ImageIcon(this.getClass().getResource(menuBanner));

		ii.paintIcon(this, g, 0, 0);

		//Main Frame
		g.setColor(new Color(0, 0, 0));
		g.fillRect(25, 25, GAME_WIDTH-50, GAME_HEIGHT-75);
		g.setColor(Color.white);
		g.drawRect(25, 25, GAME_WIDTH-50, GAME_HEIGHT-75);

		//Attack
		g.setColor(new Color(5, 75, 25));
		g.fillRect(100, 100, 125, 300);
		g.setColor(Color.white);
		g.drawRect(100, 100, 125, 300);

		//Defend
		g.setColor(new Color(5, 25, 25));
		g.fillRect(GAME_WIDTH-400, 100, 125, 300);
		g.setColor(Color.white);
		g.drawRect(GAME_WIDTH-400, 100, 125, 300);

		Font small = new Font("Helvetica", Font.BOLD, 14);
		FontMetrics metr = g.getFontMetrics(small);

		g.setColor(Color.white);
		g.setFont(small);


		g.drawString("Attackers",100,95);
		g.drawString("Defenders",GAME_WIDTH-400,95);


		// Outer Box for users list
		g.setColor(new Color(0, 255, 0));
		g.fillRect(610, 100, 140, GAME_HEIGHT-300);
		g.setColor(Color.white);
		g.drawRect(610, 100, 140, GAME_HEIGHT-300);


		//Box for Back
		g.setColor(new Color(0, 32, 48));
		g.fillRect(265, GAME_WIDTH/2 -50, 100, 50);
		g.setColor(Color.white);
		g.drawRect(265, GAME_WIDTH/2 - 50, 100, 50);

		//Box for Start
		g.setColor(Color.red);
		g.fillRect(265, GAME_WIDTH/2 - 100, 100, 50);
		g.setColor(Color.white);
		g.drawRect(265, GAME_WIDTH/2 - 100, 100, 50);

		//Box for Defender
		g.setColor(new Color(0, 32, 48));
		g.fillRect(265, GAME_WIDTH/2 - 200, 100, 50);
		g.setColor(Color.white);
		g.drawRect(265, GAME_WIDTH/2 -200, 100, 50);

		//Box for Attacker
		g.setColor(Color.red);
		g.fillRect(265, GAME_WIDTH/2 -250, 100, 50);
		g.setColor(Color.white);
		g.drawRect(265, GAME_WIDTH/2 -250, 100, 50);


		//Box for Send
		g.setColor(Color.blue);
		g.fillRect(625, GAME_WIDTH/2 + 50, 75, 75);
		g.setColor(Color.white);
		g.drawRect(625, GAME_WIDTH/2 + 50, 75, 75);

		g.drawString("Back", (GAME_WIDTH/2 - 100), 
				((GAME_HEIGHT/3*2)-20));    

		g.drawString("Start", (GAME_WIDTH/2 - 99), 
				((GAME_HEIGHT/3*2) - 70));

		g.drawString("Send", (645), 
				(GAME_WIDTH/2 + 90));

		g.drawString("Attack", (GAME_WIDTH/2 - 105), 
				((GAME_HEIGHT/3) - 20));

		g.drawString("Defend", (GAME_WIDTH/2 - 107), 
				(GAME_HEIGHT/3 + 30));

		/*
		   String test = "Key pressed is " + last_x + " " + last_y;
		   g.drawString(test,100,100);
		 */


	}


}

////

private class HandleInput implements ActionListener
{
	HandleInput()
	{
		Input1.addActionListener(this);
		Input2.addActionListener(this);
		Input3.addActionListener(this);
	}

	public void actionPerformed(ActionEvent evt) 
	{


	}
} 






////


/************************************************************************************

  Mouse Adapter Class

 ************************************************************************************/

private class Madapter extends MouseAdapter {

	public void mouseClicked(MouseEvent e){
		last_x = e.getX();
		last_y = e.getY();

		if(menu){
			if((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 20 && last_y > GAME_WIDTH/2 - 30))
			{
				click.play(0);
				serversMenu();

			}

			else if ((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 70 && last_y > GAME_WIDTH/2 + 20)) {
				click.play(0);
				optionsMenu();
			}

			else if ((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 120 && last_y > GAME_WIDTH/2 + 70)) {
				//click.play(0);
				bgm.stop();
				System.exit(1);
			}
		}

		else if(options){

			if ((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 120 && last_y > GAME_WIDTH/2 + 70)) {
				click.play(0);
				removePanel("options");
				base.change("menu");
				menu=true;
				options=false;
			}
			if ((last_x < 400 && last_x > 250) && (last_y < GAME_WIDTH/2 && last_y > GAME_WIDTH/2 - 50)) {
				click.play(0);

				FileUploader files = new FileUploader();
				file_name = files.UploadFile();
				image_name = files.returnFile();
				showfile = true;

				if(file_name != "Invalid")
					showImage = true;

				optionsMenu();

				showfile = false;
				musicshowfile = false;
			}

			if ((last_x < GAME_WIDTH-300 && last_x > 400) && (last_y < GAME_WIDTH/2 && last_y > GAME_WIDTH/2 - 50)) {
				click.play(0);

				FileUploader files2 = new FileUploader();
				musicfile_name = files2.UploadFile();
				musicshowfile = true;

				optionsMenu();

				musicshowfile = false;
				showfile = false;
			}
		}
		else if(servers){

			//Join Game
			if ((last_x < 400 && last_x > 250) && (last_y < 365 && last_y > 325)) {

				System.out.println("Join Game");
				click.play(0);

				if (invite){
					removePanel("options");
					preGameMenu();
					menu=false;
					options=false;
					preGame=true;
				}
			}

			else if ((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 70 && last_y > GAME_WIDTH/2 + 20)) {
				click.play(0);
				
				try{
					gameID=UserRole.createNewGame(getLoginName);
					
				}
				catch (Exception ex){}
				hostname = getLoginName;
				
				
				
				removePanel("options");
				preGameMenu();
				menu=false;
				options=false;
				preGame=true;
			}

			if ((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 120 && last_y > GAME_WIDTH/2 + 70)) {
				click.play(0);
				removePanel("options");
				base.change("menu");
				menu=true;
				options=false;
			}
		}

		else if(preGame){

			//Start
			if((last_x < 365 && last_x > 265) && (last_y < GAME_WIDTH/2 - 50 && last_y > GAME_WIDTH/2 - 100))
			{ 
				click.play(0);
				
				

				
				
				//if (!getLoginName.equals(hostname)){
					System.out.println("Detected click on start Game");
					
					
	
					
					try {
						//UserRole.sendFile();
						UserRole.startGameSession(getLoginName);
						Menu.startGame();
					}//call attacker function
					catch (Exception ex){}  
				//}

			}

			//Chat Send
			else if ((last_x < 700 && last_x > 625) && (last_y < 525 && last_y > 450)) {
				click.play(0);
				try {
					if (message.getText().compareTo("") > 0){
						SendSocketsList.sendToAll(message.getText());
						updateChatWindow("(Me): " + message.getText());
						message.setText("");
					}
				}

				catch (Exception ex){}
			}

			//Back
			else if ((last_x < 365 && last_x > 265) && (last_y < GAME_WIDTH/2  && last_y > GAME_WIDTH/2 - 50)) {
				click.play(0);
				removePanel("test");
				serversMenu();
			}

			//Attacker
			else if ((last_x < 365 && last_x > 265) && (last_y < 200  && last_y > 150)) {
				click.play(0);
				try {
					UserRole.addAttacker(hostname,getLoginName,1);
				}//call attacker function

				catch (Exception ex){}
			}

			//Defender
			else if ((last_x < 365 && last_x > 265) && (last_y < 250  && last_y > 200)) {
				click.play(0);
				try{
					UserRole.addDefender(hostname,getLoginName,1);
				}
				catch (Exception ex){}
			}
		}
		else if(endGame){
			if ((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 120 && last_y > GAME_WIDTH/2 + 70)) {
				click.play(0);
				removePanel("options");
				base.change("menu");
				menu=true;
				options=false;
				hostname=null;
				gameID=null;
				invite=false;
				bgm.play(1);
				gameBgm.stop();
			}

		}
		
		
		else if(login)
		{	//exit
			if ((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 120 && last_y > GAME_WIDTH/2 + 70)) {

				System.exit(1);
			}
			
			
			//forgotpassword
			if(!newuser)
			{
				
				//if forgot password is clicked
				if((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 && last_y > GAME_WIDTH/2 - 50)) {   
					click.play(0);
					/*forgot = true;
					loginMenu();
					forgot = false;*/
					status = new TCPClient();
					getLoginName = Name1_.getText();
					System.out.println("in forgot password: " +getLoginName);
					status.setParameters(getLoginName,"","");
					System.out.println("done set params for forgot");
					try
					{  //userlogin is 1, newuser int 2.
				
					status.establishConnection(3);

						System.out.println("done forgot password");

						}

						catch (Exception ex)
						{
							System.out.println(e);
						}
					
					
				}
				
				
				else if((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 - 60 && last_y > GAME_WIDTH/2 - 110)) 
				{

						               newuser = true;
						               login = true;
						               menu = false;
						               options = false;

						               removePanel("login");
						               base.change("login");

						               loginMenu();
					
				}
			}
			
			if ((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 60 && last_y > GAME_WIDTH/2 + 10)) 
			{
				click.play(0);
				getLoginName = Name1_.getText();
				getLoginPwd = Pwd1_.getText();
				
				status = new TCPClient();
				
				if(newuser)
				{
					newUser = getLoginName;
					newPwd = getLoginPwd;
					newEmail = email.getText();
					if (!(newUser.equals("") || newPwd.equals("") || newEmail.equals(""))) {
					try
					{
						System.out.println("New password");
						//UserRole.createNewUser(newUser,newPwd,newEmail);
						status.setParameters(getLoginName,getLoginPwd,newEmail);
						if (status.establishConnection(2) == 1){
							removePanel("login");
							base.change("menu");
							menu=true;
							options=false;
							login = false;
							System.out.println("Done new password");
						}
					} 
					catch (Exception ex) {}
					}
				}// end if
		
		
				else {
					System.out.println("Normal User");
					System.out.println("login name is " + getLoginName + ".");

					if (!(getLoginName.equals("") || getLoginPwd.equals(""))) {

						int i=0;
						
						status.setParameters(getLoginName,getLoginPwd,"");
				
						try
						{  //userlogin is 1, newuser int 2.
				
							if (status.establishConnection(1) == 1){
							removePanel("login");
							base.change("menu");
							menu=true;
							options=false;
							login = false;
						}

						//else show wrong password dialogue.

						System.out.println("hostname is " + hostname + " and user is " + getLoginName);

						}

						catch (Exception ex)
						{
							System.out.println(e);
						}
					}
			}
		

			}
		}//end login else
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	}
}


}






